package com.example.audio;

import com.example.listenanddriveproject.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;

public class AudioOut {
	
    private MediaPlayer mediaPlayer;
    private AudioManager audioManager;
    
    /***
     * Main constructor
     * @param context, the context where the audio is launched
     */
	public AudioOut(Context context) {
		android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
		
		mediaPlayer = MediaPlayer.create(context, R.raw.beep);
		mediaPlayer.setLooping(true);
		mediaPlayer.setVolume((float) 0.5, (float) 0.5);
		
		audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
	}
	
	/***
	 * Pause the sound
	 */
	public void pauseSound() {
		mediaPlayer.pause();
	}
	
	/***
	 * stop the sound
	 */
	public void stopSound() {
		mediaPlayer.stop();

	}
	
	/***
	 * Start the sound
	 */
	@SuppressWarnings("deprecation")
	public void startSound() {
		if(audioManager.isWiredHeadsetOn())
			mediaPlayer.start();
	}
	
	/***
	 * Check if the sound is playing
	 * @return boolean, true if playing false else
	 */
	public boolean isPlaying() {
		return mediaPlayer.isPlaying();
	}
	
	/***
	 * Check if the headset is plugged
	 */
	@SuppressWarnings("deprecation")
	public void checkHeadSet() {
		if((!audioManager.isWiredHeadsetOn())&&(mediaPlayer.isPlaying())){
			mediaPlayer.pause();
		}
		
	}

}
